package 第一章基础算法.高精度.id791高精度加法;

import java.util.Scanner;

/**
 * 例子：
 *      num1 = 6485342
 *      num2 = 985498975
 * 方法一：
 *      竖式相加，边加边进位
 * 方法二：
 *      先补0对齐，相加进位
 */
public class Main {
    static StringBuilder res = new StringBuilder();
    public static void main(String[]args){
        //处理输入
        Scanner scanner = new Scanner(System.in);
        String num1 = scanner.next();
        String num2 = scanner.next();

        //求和
        //方法一
        decimalSum1(num1,num2);
        //方法二
        decimalSum2(num1,num2);

        //处理输出
        System.out.println(res.reverse());
    }

    public static void decimalSum1(String num1,String num2){
        int len1 = num1.length();
        int len2 = num2.length();
        //反转一下方便遍历
        StringBuilder s1 = new StringBuilder(num1);
        StringBuilder s2 = new StringBuilder(num2);
        num1 = s1.reverse().toString();
        num2 = s2.reverse().toString();

        //进位
        int carry = 0;
        int len = Math.max(len1,len2);
        for(int i = 0 ;i < len;i++){
            int cur = 0;
            if(i > len1-1){
                cur = (num2.charAt(i)-'0') + carry;
            }
            else if(i > len2-1){
                cur = (num1.charAt(i)-'0') + carry;
            }else {
                cur = num1.charAt(i)-'0' + num2.charAt(i)-'0' + carry;
            }
            carry = cur / 10;
            res.append(cur%10);
        }
        if(carry > 0){
            res.append(1);
        }
    }

    public static void decimalSum2(String num1,String num2){
        int len1 = num1.length();
        int len2 = num2.length();
        //补齐0
        int len = Math.max(len1,len2);
        StringBuilder s1 = new StringBuilder(num1);
        StringBuilder s2 = new StringBuilder(num2);
        int x = len-len1;
        int y = len-len2;
        while (x > 0){
            s1.insert(0,"0");
            x--;
        }
        while (y > 0){
            s2.insert(0,"0");
            y--;
        }
        int carry = 0;
        for(int i = len-1;i>=0;i--){
            int cur = 0;
            cur = s1.charAt(i)-'0' + s2.charAt(i)-'0'+ carry;
            carry = cur /10;
            res.append(cur%10);
        }
        if(carry > 0)res.append(1);
    }
}

